package com.zbkj.crmeb.merchant.api.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.chant.model.ChannelActivity;
import com.zbkj.crmeb.chant.service.ChannelActivityService;
import com.zbkj.crmeb.merchant.api.response.HomePageActivityResponse;
import com.zbkj.crmeb.store.response.StoreProductResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 商户限时活动表 前端控制器
 * </p>
 *
 * @author wuzp
 * @since 2022-05-30
 */
@RestController
@RequestMapping("/api/front/mer/activity")
@Api(tags = "限时活动")
public class FontChannelActivityController extends ApiBaseController {

    @Autowired
    ChannelActivityService channelActivityService;

    @ApiOperation(value = "首页活动列表")
    @GetMapping(value = "/products")
    public CommonResult<HomePageActivityResponse> getHomePageActivityProducts(
            @ApiParam(required = true) Integer activityId, @Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest){
        if(ObjectUtils.isEmpty(activityId)){
            return CommonResult.failed("请传入activityId");
        }
        ChannelActivity channelActivity = channelActivityService.getById(activityId);
        if(null == channelActivity || 0 != channelActivity.getIsDel()){
            return CommonResult.failed("活动已经被删除");
        }
        if(channelActivity.getEndTime().before(new Date())){
            return CommonResult.failed("活动已结束");
        }
        PageInfo<StoreProductResponse> pageInfo = channelActivityService.selectActivityProductList(channelActivity,pageParamRequest,getMerId(servletRequest));
        HomePageActivityResponse response = new HomePageActivityResponse();
        response.setChannelActivity(channelActivity);
        response.setResponseCommonPage(CommonPage.restPage(pageInfo));
        return CommonResult.success(response);
    }

}

